<?php

namespace C\S\Other;

use C\L\Service;
use C\P\UCPaas;
use C\P\SMSChinese;
use C\P\SMSBao;

class Code extends Service
{
    protected function setModel()
    {
        $this->model = new \C\M\Code();
    }

    public function verify($mobile, $code, $type)
    {
        try {
            $data = $this->search(
                [
                'mobile' => $mobile,
                'type'   => $type,
                'status' => 'S',
                'code'   => $code,
                ]
            );

            if (empty($data) || $data['expire_time'] < time()) {
                  throw new \Exception('验证码有误');
            }

            if (!$this->update($data['id'], ['status' => 'Y'])) {
                throw new \Exception('code修改失败');
            }

            return true;
        } catch (\Exception $e) {
            $this->di['message']->setCodeMsg($e->getMessage());
            return false;
        }
    }

    public function send($mobile, $type)
    {
        try {
            $code = rand(111111, 999999);

            $data = [
            'code'        => $code,
            'type'        => $type,
            'mobile'      => $mobile,
            'expire_time' => 300 + time(),
            ];

            if (!$this->save($data)) {
                throw new \Exception('短信发送失败');
            }

            $smsDriver = $this->di['config']->get('config')->sms_driver;
            $result = null;
            switch ($smsDriver) {
                case 'ucpass';
                    $result = $this->sendSMSByUCPass($mobile, $type, $code);
                break;
                case 'sms_chinese':
                    $result = $this->sendSMSBySMSChinese($mobile, $type, $code);
                    break;
                case 'sms_bao':
                    $result = $this->sendSMSBySMSBao($mobile, $type, $code);
                    break;
                default:
                    throw new \Exception('未知短信服务商');
            }

            if (!$result) {
                throw new \Exception('短信发送失败');
            }

            return true;
        } catch (\Exception $e) {
            $this->di['message']->setSerMsg($e->getMessage());

            return false;
        }
    }

    private function sendSMSBySMSChinese($mobile, $type, $code)
    {
        $config = $this->di['config']->get('config')->sms_chinese;

        $provider = new SMSChinese($config['uid'], $config['key']);
        $content = str_replace('xxxxxx', $code, $config['templates'][$type]);
        $result = $provider->send($mobile, $content);

        return $result;
    }

    private function sendSMSByUCPass($mobile, $type, $code)
    {
        $config = $this->di['config']->get('config')->ucpass;

        $provider = new UCPaas(
            $config['app_id'],
            $config['account_sid'],
            $config['auth_token'],
        );
        $result = $provider->send(
            $mobile,
            $config['templates'][$type],
            [$code],
        );

        return $result;
    }

    private function sendSMSBySMSBao($mobile, $type, $code)
    {
        $config = $this->di['config']->get('config')->sms_bao;

        $provider = new SMSBao(
            $config['username'],
            $config['password'],
        );
        $content = str_replace('xxxxxx', $code, $config['templates'][$type]);
        $result = $provider->send($mobile, $content);

        return $result;
    }
}
